import api from "../../api"
import router, { DynamicRoutes } from "../../router/index"
import dynamicRouter from "../../router/dynamic"
import { recursionRouter, setDefaultRoute } from "../../utils/recursion-router"

export default {
    namespaced: true,
    state: {
        //获取本地存储数据，没有取默认值
        admainName: localStorage.getItem("admainName") || '',
        permissionList: null,
        sidebarMenu: [],// 导航菜单
        currentMenu: '' // 高亮
    },
    getters: {},
    mutations: {
        setAdmainName(state, value) {
            state.admainName = value;
        },
        SET_PERMISSION(state, routes) {
            state.permissionList = routes;
        },
        CLEAR_PERMSSION(state) {
            state.permissionList = null;
        },
        SET_MENU(state, menu) {
            state.sidebarMenu = menu;
        },
        CLEAR_MENU(state) {
            state.sidebarMenu = []
        }
    },
    // 异步访问
    actions: {
        async FETCH_PERMISSION({ commit, state }) {
            let permissionList = await api.permission();
            // 筛选
            let routes = recursionRouter(permissionList, dynamicRouter);
            let MainContainer = DynamicRoutes.find(v => v.path === "");
            let children = MainContainer.children;
            children.push(...routes)

            // 生成菜单
            commit("SET_MENU", children);

            // 设置默认路由
            setDefaultRoute([MainContainer]);
            // 初始化路由
            let initialRoutes = router.options.routes;
            router.addRoutes(DynamicRoutes);
            commit("SET_PERMISSION", [...initialRoutes, ...DynamicRoutes])
        }
    }
}